var app = getApp();
var https = "https://www.moreclub.cn/";
var http2 = 'http://59.110.53.149:8088/'
var htp = 'http://more-image.oss-cn-beijing.aliyuncs.com/pinzuominiapp/pinzuoall_bg_';
var istrue = false;
var isInto = true;
var idn;
var showdiv = false

var add0 = function (m) {
  return m < 10 ? '0' + m : m
}
var time = function (prefeerTime) {
  var time = new Date(prefeerTime);
  // var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  // var s = time.getSeconds();
  // return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  return add0(m) + '月' + add0(d) + '日' + ' ' + add0(h) + ':' + add0(mm)
}

var getList = function (that,mid) {
  var uid = wx.getStorageSync('uid');
  wx.request({
    url: https+'v2/ugc/open/listsbymid?uid=' + uid + '&mid=' + mid + '&page=0' + '&pageSize=20',
    data: {
      page: 0,
      pageSize: 20
    },
    success: function (res) {
      console.log(res)
      if (res.data.success) {
        var pubData = res.data.data.ugcs
        try {
          wx.setStorageSync('pubData', pubData);
        } catch (e) {
        }

        var pubList = [];//创建一个新数组
        for (var key in pubData) {
          var fromThumb = pubData[key].thumb;//头像
          // if (pubData[key].prefeerPictrues) {
          //   var photos = pubData[key].prefeerPictrues;//图片
          //   var photo = photos.split(",");
          // }
          var photo = pubData[key].pictures //数组
          var chainInters = pubData[key].chainInters
          if (pubData[key].content){
            var prefeerContent = pubData[key].content;//内容
            var contentArr = prefeerContent.split('\n');
          }
         
          var nickName = pubData[key].nickName;
          if (pubData[key].likeDto.userLikes) {
            var userLikes = pubData[key].likeDto.userLikes.reverse();//点赞人显示
          } else {
            var userLikes = pubData[key].likeDto.userLikes
          }
          
          var prefeerTime = pubData[key].createTime;//时间
          var pid = pubData[key].ugcId;
          var isClick = pubData[key].likeDto.clicked;
          var likeTimes = pubData[key].likeDto.likeTimes;
          var merchantName = pubData[key].merchantName;
          var chainInter = pubData[key].chainInter;
          //var subType = pubData[key].subType;
          //var sourceFrom = pubData[key].sourceFrom;//more出品
          var uid1 = pubData[key].uid;
          //var type1 = pubData[key].type;
          var commentCount = pubData[key].commentCount;//评论人数
          var comments = pubData[key].comments;//评论内容
          time(prefeerTime)
          var dataTime = time(prefeerTime)

          // for (var i = 0; i < pubData[key].likeDto.userLikes.length;i++){
          //   if (uid == pubData[key].likeDto.userLikes[i].uid) {
          //       reday = true
          //      break;
          //   }else{
          //      reday = false
          //   }
          // }


          var obj = {};//创建新对象
          obj.fromThumb = fromThumb;
          obj.nickName = nickName;
          //obj.prefeerContent = prefeerContent;
          obj.dataTime = dataTime;
          //obj.photos = photos;
          obj.photo = photo;
          obj.isClick = isClick;
          obj.chainInter = chainInter;
          obj.likeTimes = likeTimes;
          obj.merchantName = merchantName;
          //obj.subType = subType;
         // obj.sourceFrom = sourceFrom;
         // obj.type1 = type1;
          obj.pid = pid;
          obj.uid1 = uid1;
          obj.contentArr = contentArr;
          obj.userLikes = userLikes;
         obj.commentCount = commentCount;
          obj.comments = comments;
          pubList.push(obj);
        };
        console.log(pubList)
        if (pubList != null) {

          var oldlist = that.data.totallist;
          for (var i = 0; i < pubList.length; i++) {
            oldlist.push(pubList[i])
          }
        }
        that.setData({
          pubList: pubList,
          totallist: oldlist,
          uid: uid
        })

      }
    }
  })
}


Page({
  data:{
    totallist: [],
    showBack: false
  },
  onLoad:function(options){
    var that = this;
    var mid = options.mid
    var uid = wx.getStorageSync('uid');

   
    
      if (!isInto) {
        var total = wx.getStorageSync('total');
        that.setData({
          totallist: total
        })
      } else {
        getList(that,mid)
      }

  },

  yulan:function(e){
    var idn = e.currentTarget.dataset.idn;
    wx.previewImage({
      current: 'http://more-user.oss-cn-beijing.aliyuncs.com/2017/8/31/1167343732996o2n2tpWiXKsx.jpg', 
      urls: ['http://more-user.oss-cn-beijing.aliyuncs.com/2017/8/31/1167343732996o2n2tpWiXKsx.jpg'] 
    })
  },


  showdiv: function (e) {
    var that = this;
    idn = e.currentTarget.dataset.idn
    that.setData({
      showdiv: true,
      idn: idn
    })
  },
  hidediv: function () {
    var that = this;
    that.setData({
      showdiv: false
    })
  },

  deleteBtn: function (e) {
    //删除动态
    var that = this
    var access_token = wx.getStorageSync('inToken');

    console.log(e)
    var pid = e.currentTarget.dataset.pid
    wx.request({
      url: https + 'v2/ugc/delete/' + pid + '?access_token=' + access_token,
      method: 'DELETE',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if (res.data.data) {
          var totallist = that.data.totallist
          totallist.splice(idn, 1)
          that.setData({
            totallist: totallist
          })
        }
      }
    })
    that.setData({
      showdiv: false
    })
  },


   praiseBtn: function (e) {
 
    var that = this;
    var uid = wx.getStorageSync('uid');
    var access_token = wx.getStorageSync('inToken');
    var total = that.data.totallist;
    var thumb = wx.getStorageSync('thumb');
    var nickName = wx.getStorageSync('myUsername');
    var idx1 = e.currentTarget.dataset.idx;
    isInto = false

    //  wx.request({
    //    url: https + 'v1/information/aordeliketime?uid=' + uid + '&fid=' + total[idx1].pid + '&type=' + total[idx1].type1 + '&access_token=' + access_token,
    //    success: function (res) {
    //      console.log(res)
    //    }
    //  })

    if (total[idx1].isClick && total[idx1].userLikes != '') {

      for (var j in total[idx1].userLikes) {
        if (total[idx1].userLikes[j].uid == uid) {
          var n = j;
          total[idx1].userLikes.splice(n, 1)

          total[idx1].userLikes = total[idx1].userLikes
          total[idx1].isClick = false
          total[idx1].likeTimes -= 1
          that.setData({
            totallist: total,
            idx1: idx1,
            isClick: total[idx1].isClick
          })
        }
      }
      try {
        wx.setStorageSync('total', total);
      } catch (e) {
      }

    } else {
      if (total[idx1].userLikes) {
        var o = {}
        o.thumb = thumb;
        o.uid = uid;
        o.nickName = nickName;
        total[idx1].userLikes.push(o)
        total[idx1].isClick = true
        total[idx1].likeTimes += 1
        that.setData({
          totallist: total,
          idx1: idx1,
          isClick: total[idx1].isClick
        })
        try {
          wx.setStorageSync('total', total);
        } catch (e) {
        }
      } else {
        var arr = []
        var o = {}
        o.thumb = thumb;
        o.uid = uid;
        o.nickName = nickName;
        arr.push(o)
        total[idx1].userLikes = arr
        total[idx1].isClick = true
        total[idx1].likeTimes += 1
        console.log(total)
        that.setData({
          totallist: total,
          idx1: idx1,
          isClick: total[idx1].isClick
        })
        try {
          wx.setStorageSync('total', total);
        } catch (e) {
        }
      }

    }


  }
})